package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.WarnType;
import com.yqg.protection.service.IWarnTypeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * WarnTypeController 前端控制器
 * @author zxf
 * @since 2024-08-05
 */
@RestController
@RequestMapping("/warn-type")
@Api(tags = "报警类型")
public class WarnTypeController {

    @Resource
    private IWarnTypeService warnTypeService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改报警类型")
    public Result save(@RequestBody WarnType warnType) {
        warnType.setIsDelete(0);
        return Result.success(warnTypeService.saveOrUpdate(warnType));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @SysLog("删除报警类型")
    public Result delete(@PathVariable Integer id) {
        WarnType warnType =warnTypeService.getById(id);
        warnType.setIsDelete(1);
        return Result.success(warnTypeService.saveOrUpdate(warnType));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<WarnType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<WarnType> list=  warnTypeService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(warnTypeService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           String name) {
        QueryWrapper<WarnType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.like("warn_type_name", name);
        queryWrapper.orderByDesc("id");
        return Result.success(warnTypeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

